﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using Unscrambler.Comparers;

namespace Unscrambler.Controls
{
    public class AnswerCanvas : Canvas
    {

        public static readonly DependencyProperty WordProperty = DependencyProperty.Register(
            "Word", typeof(string), typeof(AnswerCanvas),
            new PropertyMetadata(string.Empty),
            null);

        public static readonly DependencyProperty CurrentAnswerProperty = DependencyProperty.Register(
            "CurrentAnswer", typeof(string), typeof(AnswerCanvas),
            new PropertyMetadata(string.Empty),
            null);

        public static readonly DependencyProperty IsCorrectProperty = DependencyProperty.Register(
            "IsCorrect", typeof(Boolean), typeof(AnswerCanvas),
            new PropertyMetadata(false), null);

        public string Word
        {
            get { return (string)this.GetValue(WordProperty); }
            set
            {
                this.SetValue(WordProperty, value);
            }
        }

        public string CurrentAnswer
        {
            get { return (string)this.GetValue(CurrentAnswerProperty); }
            set
            {
                this.SetValue(CurrentAnswerProperty, value);
            }
        }

        public bool IsCorrect
        {
            get { return (bool)this.GetValue(IsCorrectProperty); }
            set
            {
                this.SetValue(IsCorrectProperty, value);
            }
        }

        //this needs to be called from the user control any time a manipulation completes
        public void UpdateAnswer()
        {
            List<LetterTile> letters = new List<LetterTile>(this.Children.OfType<LetterTile>());
            Rect mySize = new Rect(this.RenderSize);
            LetterTileComparer comparer = new LetterTileComparer(mySize);
            letters.Sort(comparer);
            
            StringBuilder sb = new StringBuilder();
            letters.ForEach(x => sb.Append(x.Letter));
            this.CurrentAnswer = sb.ToString();

            if (this.CurrentAnswer == this.Word)
            {
                this.IsCorrect = true;
            }
            else
            {
                this.IsCorrect = false;
            }
               
        }
        

        public AnswerCanvas()
        {
                  
        }

        
    }
}
